package com.jiujichaoshi.oauth.server.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Description: 查询client信息服务类。
 *
 * @author YangLong [410357434@163.com]
 * @version V1.0
 * @date 2021/2/5
 */
@Primary
@Service
@SuppressWarnings("deprecation")
public class ExtendClientDetailsServiceImpl implements ClientDetailsService {
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {
        BaseClientDetails details = new BaseClientDetails();
        details.setClientId("sample");
        details.setClientSecret(passwordEncoder.encode("sample"));
        details.setAuthorities(new ArrayList<GrantedAuthority>(){{
            add(new SimpleGrantedAuthority("READ"));
            add(new SimpleGrantedAuthority("WRITE"));
        }});
        details.setRegisteredRedirectUri(new HashSet<String>(){{
            add("https://www.baidu.com");
        }});
        details.setScope(Collections.singletonList("all"));
        details.setAuthorizedGrantTypes(Arrays.asList("client_credentials","implicit","refresh_token","authorization_code","password","sms"));
        details.setAutoApproveScopes(Collections.singletonList("all"));
        return details;
    }
}
